package com.ssm.student.controller;

import com.ssm.student.eo.RoleEoPage;
import com.ssm.student.model.Role;
import com.ssm.student.service.RoleService;
import com.ssm.student.tool.ResponseMessage;
import com.ssm.student.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据服务接口
 */
@Api(tags = "role")
@RestController
@CrossOrigin
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "角色添加方法")
    @RequestMapping(value = "/addRole", method = RequestMethod.GET)
    public ResponseMessage addRole(Role role) {
        int IsAdd = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (role != null) {
            //(2)角色名称不能为空
            if (role.getJuesemingcheng() != null) {
                //(3)添加角色数据
                IsAdd = roleService.AddRole(role);
                if (IsAdd == 1) {
                    return Result.success("保存成功");
                } else if (IsAdd == -1) {
                    return Result.error("角色重复，不能添加");
                } else {
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }

    @ApiOperation(value = "角色删除方法")
    @RequestMapping(value = "/delRole", method = RequestMethod.GET)
    public ResponseMessage delRole(int roleid) {
        if (roleid != 0) {
            int delInt = 0;
            delInt = roleService.DeleteRole(roleid);
            if (delInt > 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } else {
            return Result.error("删除的主键不能为0请重新输入");
        }
    }

    @ApiOperation(value = "角色修改方法")
    @RequestMapping(value = "/updateRole", method = RequestMethod.GET)
    public ResponseMessage updateRole(Role role) {
        //传入的参数不能为空
        if (role != null) {
            //参数中主键不能为0
            if (role.getRoleid() != 0) {
                if (roleService.UpdateRole(role) == 1) {
                    //
                    if (role.getShifouqiyong()!=null){
                      if (role.getShifouqiyong().length()<5){
                          return Result.success("更新成功");
                      }
                    }
                }
            }
        }
        else {
            return Result.error("更新传入参数不正确");
        }
        return Result.error("更新传入参数不正确");
    }
    @ApiOperation(value = "根据条件获取信息")
    @RequestMapping(value = "/getAllRole",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    private  ResponseMessage getAllRole(@RequestBody RoleEoPage roleEoPage){
        Map<String, List> map=new HashMap<>();
        List<RoleEoPage> listRole=roleService.getWhereAll(roleEoPage);
        map.put("list",listRole);
        return Result.success("200",map);
    }
    @ApiOperation(value = "根据条件获取信息")
    @RequestMapping(value = "/getAllJue",produces = "application/json;charset=utf-8",method = RequestMethod.GET)
    private  ResponseMessage getAllJue(@RequestParam String juesemingcheng){
        Map<String, List> map1=new HashMap<>();
        List<RoleEoPage> listperson1=roleService.getWhereJue(juesemingcheng);
        if (listperson1!=null) {
            map1.put("list", listperson1);
            return Result.success("200",map1);
        }else {
            return Result.error("用户名或密码错误");
        }

    }
}